package com.xing.crm.service.impl;

import com.xing.crm.mapper.ProProductMapper;
import com.xing.crm.mapper.ProSkuMapper;
import com.xing.crm.pojo.*;
import com.xing.crm.service.ProProductService;
import com.xing.crm.service.ProShopService;
import com.xing.crm.utils.ParseEditorUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.File;
/**
 * @author Mr.L
 * @version V1.0
 * @date 2021年07月21日 17:36
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class ProProductServiceImpl implements ProProductService {
	
	@Resource
	private ProProductMapper proProductMapper;
	@Resource
	private ProShopService proShopService;
	@Resource
	private ProSkuMapper proSkuMapper;
	@Override
	public PageResult<ProProductPoJo> list(ProProductPoJo productPoJo, Integer page, Integer limit)
	{
		Map params = new HashMap(0);
		params.put("product", productPoJo);
		params.put("start", (page - 1) * limit);
		params.put("limit", limit);
		PageResult<ProProductPoJo> result = new PageResult();
		result.setCode(0);
		result.setMsg("查询成功");
		result.setData(this.proProductMapper.list(params));
		result.setCount(this.proProductMapper.count(params).intValue());
		return result;
	}
	
	@Override
	public int addFilesPath(ProProductPoJo productPoJo)
	{
		return this.proProductMapper.addFilesPath(productPoJo);
	}
	
	@Override
	public Map<String, Object> edit(ProProductPoJo productPoJo)
	{
		if (productPoJo.getId() == null) {
			this.proProductMapper.add(productPoJo);
		} else {
			this.proProductMapper.edit(productPoJo);
		}
		Integer id = productPoJo.getId();
		if ((id != null) &&
				(!productPoJo.getFilesPath().isEmpty())) {
			addFilesPath(productPoJo);
		}
		Map result = new HashMap(0);
		result.put("code", Integer.valueOf(0));
		result.put("msg", "操作成功");
		result.put("data", productPoJo.getId());
		return result;
	}
	
	@Override
	public Map<String, Object> delete(Integer id)
	{
		/* 产品封面、视频、图文详情 */
		ProProductPoJo filesAlbum = proProductMapper.albumFile(id);
		
		/* 详情图 */
		List<String> files = proProductMapper.allFiles(id);
		
		/* 合并2个种图片数据 */
		if(filesAlbum != null) {
			files.add(filesAlbum.getCover());
			if( !StringUtils.isEmpty(filesAlbum.getVideoPath())){
				files.add(filesAlbum.getVideoPath());
			}
			String des = filesAlbum.getDescription();
			if(!StringUtils.isEmpty(des)) {
				files.addAll(ParseEditorUtil.getImgStr(des));
			}
		}
		//得到商品的sku所有文件
		List<String> filesSku = proSkuMapper.allFileOfProduct(id);
		files.addAll(filesSku);
		
		//处理文件
		String dir = System.getProperty("user.dir") + File.separator;
		File file;
		for(String path: files)
		{
			if(path != null){//上面的files在处理的时候，有可能放进去的是空的，下面的new File就会发疯
				file = new File(dir, path);
				if(file.exists()) {
					file.delete();
					System.out.println("文件"+file+"存在，删除！");
				}
			}
			
		}
		Map result = new HashMap(0);
		result.put("code", Integer.valueOf(0));
		result.put("msg", "操作成功");
		if (id != null)
		{
			/* 删除店铺商品关联记录 */
			this.proProductMapper.deleteShopProduct(id);
			/* 删除产品附件表记录 */
			this.proProductMapper.deleteFiles(id);
			/* 删除产品记录 */
			this.proProductMapper.delete(id);
			//删除sku记录
			this.proSkuMapper.deleteOfProduct(id);
		}
		return result;
	}
	
	@Override
	public ProProductPoJo detail(Integer id)
	{
		return this.proProductMapper.detail(id);
	}
	
	@Override
	public List<String> allFiles(Integer productId)
	{
		return this.proProductMapper.allFiles(productId);
	}
	
	/**
	 * 返回商品列表，注意这里用的双查询结果合并 ，不能分页
	 * @param user
	 * @param isManager
	 * @param isClerk
	 * @param cid
	 * @param page
	 * @param limit
	 * @return
	 */
	@Override
	public PageResult<ProProductPoJo> getShopProducts(WebUser user, Boolean isManager, Boolean isClerk, Integer cid, int page, int limit) {
		PageResult<ProProductPoJo> result = new PageResult();
		result.setCode(0);
		result.setMsg("没有权限");
		result.setData(new ArrayList());
		ProProductPoJo product = new ProProductPoJo();
		/* 设置产品分类 */
		if(cid != null) {
			product.setCategoryId(cid);
		}
		boolean isAuth = (isManager != null && isManager) || (isClerk != null && isClerk);

		if(isAuth) {
			ProShop shop = proShopService.getShopByUid(user);
			if(shop != null) {
				Map<String, Object> params = new HashMap(0);
				params.put("sid", shop.getId());
				params.put("product", product);
				params.put("start", (page - 1) * limit);
				params.put("pageSize", limit);
				List<ProProductPoJo> sells = proProductMapper.getOnSellProducts(params);
				List<ProProductPoJo> unsold = proProductMapper.getUnsoldProduct(params);
				sells.addAll(unsold);
				result.setMsg("查询成功");
				result.setData(sells);
				result.setCount(sells.size());
			}
		}
		return result;
	}
	
	
	/**
	 * 为本店添加在售商品
	 * @param user
	 * @param pid
	 * @param isManager
	 * @return
	 */
	@Override
	public PageResult shopOnSaleProduct(WebUser user, Integer pid, Boolean isManager) {
		PageResult result = new PageResult();
		result.setCode(-1);
		result.setMsg("没有权限");
		/* 获取所属店铺 */
		ProShop shop = proShopService.getShopByUid(user);
		if(pid != null && shop != null) {
			/* 检查产品是否已存在 */
			int exist = proProductMapper.checkProductExist(shop.getId(), pid);
			result.setMsg("重复添加，已拒绝");
			if(exist == 0) {
				if(isManager != null && isManager) {
					if(proProductMapper.shopOnSaleProduct(shop.getId(), pid) > 0) {
						result.setCode(0);
						result.setMsg("添加在售商品成功");
					}
				}
			}
		}
		
		return result;
	}
	
	/**
	 * 取消本店在售商品
	 * @param user
	 * @param pid
	 * @param isManager
	 * @return
	 */
	@Override
	public PageResult cancelSaleProduct(WebUser user, Integer pid, Boolean isManager) {
		PageResult result = new PageResult();
		result.setCode(-1);
		result.setMsg("没有权限");
		/* 获取所属店铺 */
		ProShop shop = proShopService.getShopByUid(user);
		if(pid != null && shop != null) {
			if(isManager != null && isManager) {
				if(proProductMapper.cancelSaleProduct(shop.getId(), pid) > 0) {
					result.setCode(0);
					result.setMsg("成功取消店铺在售产品");
				}
			}
		}
		
		return result;
	}
	
	/**
	 * //商品列表
	 * @param user
	 * @param product
	 * @param isManager
	 * @param isClerk
	 * @param page
	 * @param limit
	 * @return
	 */
	@Override
	public PageResult<ProProductPoJo> productList(WebUser user, ProProductPoJo product, Boolean isManager, Boolean isClerk, int page, int limit) {
		List<ProProductPoJo> products = new ArrayList();
		PageResult result = new PageResult();
		result.setCode(-1);
		result.setMsg("没有权限");
		
		/* 是否店长、店员，才读取数据*/
		if((isManager != null && isManager) || (isClerk != null && isClerk)) {
			
			Map params = new HashMap(0);
			params.put("start", (page - 1) * limit);
			params.put("limit", limit);
			
			/* 获取所属店铺 */
			ProShop shop = proShopService.getShopByUid(user);
			if(shop != null) {
				params.put("sid", shop.getId());
			}else{
				params.put("sid", 0);
			}
			params.put("product", product);
			/* 获取列表 */
			products = proProductMapper.productList(params);
			result.setCode(0);
		}
		result.setData(products);
		result.setCount(products.size());
		return result;
	}
	
	//修改排序
	@Override
	public Result sort(ProProductPoJo proProductPoJo){
		Result result = new Result();
		int affectedRows = 0;
		if (proProductPoJo.getId() != null) {
			affectedRows = this.proProductMapper.sort(proProductPoJo);
		}
		result.setCode(affectedRows > 0 ? 0 : 1);
		result.setMsg(affectedRows > 0 ? "修改成功" : "没有执行成功");
		return result;
	}
	
	//切换显示
	@Override
	public Result toggleShow(Integer id){
		Result result = new Result();
		int affectedRows = 0;
		if (id != null) {
			affectedRows = this.proProductMapper.toggleShow(id);
		}
		result.setCode(affectedRows > 0 ? 0 : 1);
		result.setMsg(affectedRows > 0 ? "修改成功" : "没有执行成功");
		return result;
	}
}
